package com.wenli.lt.controller;


import com.wenli.lt.entity.Orders;
import com.wenli.lt.service.IOrdersService;
import com.wenli.lt.util.CommonResponse;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lt
 * @since 2021-04-02
 */
@RestController
@RequestMapping("/orders")
@Api(tags = "订单")
public class OrdersController {
    @Autowired
    IOrdersService iOrdersService;

    @PostMapping("addOrders")
    @ApiOperation("添加至购物车  加入购买")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "informationInfo",value = "发起购买发送信息"),
            @ApiImplicitParam(name = "sendStudentNumber",value = "发送人学号"),
    })
    public CommonResponse addOrders(@RequestBody @ApiParam(name = "orders", value = "订单表") Orders orders
            ,String informationInfo,String sendStudentNumber
    ) {

        if ( iOrdersService.addOrders(orders,informationInfo,sendStudentNumber)){
            return new CommonResponse("添加成功");
        }else {
            return new CommonResponse(CommonResponse.ERROR_CODE,"添加失败，请联系管理员");
        }
    }
    @PostMapping("updateOrders")
    @ApiOperation("更改订单状态  卖家拒绝购买发送消息 买家发起购买发送消息  1 发起购买  2 购买成功  3 购买失败")
    //购买失败回复消息可不传
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ordersId",value = "订单id",required = true),
            @ApiImplicitParam(name = "stauts",value = "订单状态",required = true),
            @ApiImplicitParam(name = "informationInfo",value = "发起购买发送信息"),
            @ApiImplicitParam(name = "sendStudentNumber",value = "如果是发起购买 传发起信息人学号  "),
    })
    public CommonResponse updateOrders(String ordersId,Integer stauts,String informationInfo,String sendStudentNumber){
        if (iOrdersService.updateOrders(ordersId,stauts,informationInfo,sendStudentNumber)){
            return new CommonResponse("修改成功!");
        }else {
            if (stauts == 2){
                return new CommonResponse("商品已经卖完");
            }else {
                return new CommonResponse("修改失败,请联系管理员");
            }
        }
    }

    @PostMapping("deleteOrders")
    @ApiOperation("删除  购物车(所有 订单 你懂的)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ordersId",value = "订单id"),
    })
    public CommonResponse deleteOrders(String ordersId){
        if ( iOrdersService.deleteOrders(ordersId)){
            return new CommonResponse("删除成功");
        }else {
            return new CommonResponse(CommonResponse.ERROR_CODE,"删除失败，请联系管理员");
        }
    }
}
